'''
Created on Apr 25, 2011

@author: Igor
'''

from common import BaseRequestHandler
from lib.dbobjects import Profiler, Game
from challenges.challenge_helper import game_setup, get_winner
from google.appengine.api.users import User
from google.appengine.api import users

class Level3Page(BaseRequestHandler):
    '''
    This is a one-on-one texas hold'em game
    '''

    def get(self):
        if (users.is_current_user_admin()):
            self.redirect('/admin')
        else:
            self.display()
    
    def post(self):
        games_query = Game.all().filter("user =",
                users.get_current_user()).order('-modified')
        game = games_query.get()
        bet = self.request.get('bet', None)
        if bet is not None:
            bet_amount = int(bet)
            profilers_query = Profiler.all().filter("user =",
                    users.get_current_user()).order('-modified')
            profiler = profilers_query.get()
            # If somehow the bet was bigger than the money, go home
            if profiler.money < self.LEVEL3_CUTOFF or \
                    bet_amount > profiler.money:
                self.redirect('/')
                return
            if game is None:
                game = Game(user=users.get_current_user(),
                            bet_amount=bet_amount)
            game.bet_amount = bet_amount
            profiler.money -= bet_amount
            profiler.put()
            game_setup(game)
            game.round = 1
        elif game.round == 3:
            profilers_query = Profiler.all().filter("user =",
                    users.get_current_user()).order('-modified')
            profiler = profilers_query.get()
            casino_query = Profiler.all().filter("user =", 
                    User('Casino')).order('-modified')
            casino = casino_query.get()
            if casino is None:
                casino = Profiler(user = User("Casino"),
                        nickname = "Robert De Niro", money = 0)
            result = get_winner(game)
            if result > 0:
                game.message = "You won! You get %d" % (game.bet_amount * 2)
                profiler.money += game.bet_amount * 2
                casino.money -= game.bet_amount
                game.bet_amount = 0
                profiler.put()
            elif result < 0:
                game.message = "You lost."
                casino.money += game.bet_amount
                game.bet_amount = 0
            else:
                game.message = "It's a tie. That means I win."
                casino.money += game.bet_amount
                game.bet_amount = 0
            casino.put()
            game.round += 1
        else:
            game.round += 1
        game.put()    
        self.display(game)
    
    def display(self, game=None):
        # Get the guest object for the current user
        profilers_query = Profiler.all().filter("user =",
                users.get_current_user()).order('-modified')
        profiler = profilers_query.get()
        
        display_form = False
        my_images = []
        your_images = []
        table_images = []
        if game is None:
            display_form = True
        else:
            for i in xrange(2):
                my_images.append("/images/card_%s.png" % game.my_cards[i])
            for i in xrange(2):
                your_images.append("/images/card_%s.png" % game.your_cards[i])    
            if game.round > 1:
                for i in xrange(game.round + 1):
                    table_images.append("/images/card_%s.png" %
                            game.table_cards[i])
            if game.round == 4:
                display_form = True
            
        template_values = {'level_name': self.LEVEL3_NAME,
                           'profiler': profiler,
                           'display_form': display_form,
                           'game': game,
                           'my_images': my_images,
                           'table_images': table_images,
                           'your_images': your_images}
        self.generate('level3.html', template_values)